#include "FloatingParticles.h"
#include "base\Engine.h"
#include "game\World.h"
#include "..\..\Constants.h"
#include "..\..\AstroMath.h"
#include <math.h>

const ds::Vec2 ACCELERATION = ds::Vec2(0,-10);
const float FP_MIN_VEL = 30.0f;
const float FP_DIFF_VEL = 10.0f;
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
FloatingParticles::FloatingParticles(ds::DynamicSpriteNode* spriteNode) : m_Sprites(spriteNode) , m_MagneticMode(false) {
}

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
FloatingParticles::~FloatingParticles(void) {
	clear();
}

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void FloatingParticles::clear() {
	Particles::iterator it = m_Particles.begin();
	while ( it != m_Particles.end() ) {
		delete it->sprite;
		it = m_Particles.erase(it);
	}
}

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void FloatingParticles::update( float elapsed ) {		
	for ( size_t i = 0; i < m_Particles.size(); ++i ) {
		FloatingParticle* fp = &m_Particles[i];
		//if ( m_MagneticMode ) {
			// move towards target position
			//fp->sprite->position = getNewPosition(fp->sprite->position,PARTICLE_DISTANCE,PARTICLE_SPEED);
		//}
		//else {
			fp->sprite->position += fp->velocity * elapsed;
			fp->velocity.x += 0.5 * ACCELERATION.x * ACCELERATION.x * elapsed;
			fp->velocity.y += 0.5 * ACCELERATION.y * ACCELERATION.y * elapsed;
		//}
		m_Sprites->addSprite(fp->sprite);
	}
	Particles::iterator it = m_Particles.begin();
	while ( it != m_Particles.end() ) {
		ds::Vec2 p = it->sprite->position;			
		if ( p.y > 800.0f ) {
			delete it->sprite;
			it = m_Particles.erase(it);
		}
		else {
			++ it;
		}
	}
}

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void FloatingParticles::init() {
}

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void FloatingParticles::add(const ds::Vec2& center,int counter,float minRadius,float maxRadius) {
	float angle = 0.0f;
	float add = 360.0f / (float)counter;
	for ( int i = 0; i < counter; ++i ) {
		FloatingParticle fp;
		float r = gEngine->random(minRadius,maxRadius);
		float x = center.x + r * cos(DEGTORAD(angle));
		float y = center.y + r * sin(DEGTORAD(angle));
		fp.sprite = new ds::Sprite(ds::Vec2(x,y),ds::Rect(0,75,20,20));
		fp.angle = angle;
		float vel = FP_MIN_VEL + gEngine->random(0.0f,FP_DIFF_VEL);
		fp.velocity = ds::MathHelper::getRadialVelocity(angle,vel);
		m_Particles.push_back(fp);
		angle += add;
	}
}

// -----------------------------------------------------------------------------
// Checks if player has picked up one of the particles
// [in] pos - the player position
// [in] dim - the dimension of the ring
// [out] - the number of picked up particles
// -----------------------------------------------------------------------------
int FloatingParticles::pickupParticles(const ds::Vec2& pos,const ds::Vec2& dim) {
	int ret = 0;
	ds::Rect r(pos.y,pos.x,dim.x,dim.y);
	ds::Vec2 center = pos;
	center.x += dim.x * 0.5f;
	center.y += dim.y * 0.5f;
	Particles::iterator it = m_Particles.begin();
	while ( it != m_Particles.end() ) {
		ds::Vec2 p = it->sprite->position;	
		float d = p.distance(center);
		if ( d < 80.0f ) {
			delete it->sprite;
			it = m_Particles.erase(it);
			++ret;
		}
		else {
			++ it;
		}
	}
	return ret;
}

